package com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.service.imp;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.exception.ServiceException;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.constants.SysUserRoleTypeEnum;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.entity.StructureSetting;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.repository.StructureSettingRepository;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.service.StructureSettingService;
import com.css.fxfzypg.modules.system.model.entity.SysUserRole;
import com.css.fxfzypg.modules.system.service.UserCacheService;
import com.css.fxfzypg.modules.maps.service.SetEarthquakeMapService;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.css.fxfzypg.zcpt.sys.service.SysRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/22
 * @Description: 结构类型设置service
 */
@Service
public class StructureSettingServiceImpl implements StructureSettingService {

    @Resource
    private StructureSettingRepository structureSettingRepository;
    @Resource
    private SUserService sUserService;
    @Resource
    private UserCacheService userCacheService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    SetEarthquakeMapService setEarthquakeMapService;
    @Resource
    SysAreaService sysAreaService;

    @Override
    public RestResponse save(StructureSetting structureSetting) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(structureSetting.getId())) {
            jsonObject.put("message", "省分id必传");
            return RestResponse.succeed(jsonObject);
        }
        StructureSetting stS = structureSettingRepository.getParamSetting(structureSetting.getId());
        if (null == stS) {
            structureSettingRepository.save(structureSetting);
        } else {
            structureSettingRepository.updateById(structureSetting);
        }
        jsonObject.put("message", "保存成功");
        return RestResponse.succeed(jsonObject);
    }

    @Override
    public String getProvinceIdByUser() throws ServiceException {
        String provinceId = "-1";
        String userId = PlatformSessionContext.getUserID();
        SysUserRoleTypeEnum role = userCacheService.getUserRoleType(userId);
        switch (role) {
            case DATA_ROLES_PROVINCE:
                SUser sUser = userCacheService.getUserFromCache(userId);
                provinceId = sUser.getProvinceId();
                return provinceId;
            case DATA_ROLES_OTHER:
                throw new ServiceException("未知角色");
        }
        return provinceId;
    }

    @Override
    public StructureSetting getByLonAndLatOfApp(String lon, String lat) {
        List<String> provinceInfoList = setEarthquakeMapService.queryProvinceByLonLat(lon, lat);
        if (provinceInfoList.size() > 0) {
            String provinceCode = provinceInfoList.get(0);
            String provinceName = provinceInfoList.get(1);
            String divisionIds = "";
            try {
                //判断省市县的名称是否符合规范
                divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(provinceName, "utf-8"),
                        "","");
                return structureSettingRepository.getParamSetting(divisionIds.split("_")[0]);
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }

    @Override
    public StructureSetting getParamSetting(String id) {
        return structureSettingRepository.getParamSetting(id);
    }
}
